﻿using System;
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using StackExchange.Redis;
using TianYuan.Core.Caching;
using TianYuan.Redis;

namespace RedisTest
{
    [TestClass]
    public class UnitTest1
    {
        String redicconfig = System.Configuration.ConfigurationManager.AppSettings["RedisConnection"].ToString();
        [TestInitialize]
        public void init()
        {
            //ICacheProvider provider = new RuntimeMemoryCacheProvider();
            //CacheManager.SetProvider(provider, CacheLevel.First);
            ICacheProvider provider = new RedisCacheProvider(new RedisClient(1, redicconfig));
            CacheManager.SetProvider(provider, CacheLevel.First);
        }

        [TestMethod]
        public void SetTest()
        {
            string key = "key1";
            ICache cache = CacheManager.GetCacher<UnitTest1>();
            cache.Set(key, "111111111");
            string value = cache.Get<string>(key);
            Assert.AreEqual(value, "111111111");

        }

        [TestMethod]
        public void SetTest1()
        {
            string key = "key2";
            ICache cache = CacheManager.GetCacher<UnitTest1>();
            cache.Set(key, new NameValue { Name = "name3", Value = 111 });
            NameValue value = cache.Get<NameValue>(key);
            Assert.IsTrue(value != null);
        }
        
        [TestMethod]
        public void SetTest2()
        {
            string key = "key3";
            ICache cache = CacheManager.GetCacher<UnitTest1>();
            cache.Set(key, "111111111", DateTime.Now.AddSeconds(100));
            string value = cache.Get<string>(key);
            Assert.AreEqual(value, "111111111");
        }
        [TestMethod]
        public void SetTest3()
        {
            string key = "key2";
            ICache cache = CacheManager.GetCacher<UnitTest1>();
            cache.Set(key, null, DateTime.Now.AddSeconds(60));
            NameValue value = cache.Get<NameValue>(key);
            Assert.IsTrue(value == null);
        }
        [TestMethod]
        public void SetRemove()
        {
            string key = "key4";
            ICache cache = CacheManager.GetCacher<UnitTest1>();
            cache.Set(key, "111111111");
            Assert.AreEqual(cache.Contains(key), true);
            string value = cache.Get<string>(key);
            cache.Remove(key);
            Assert.AreEqual(cache.Contains(key), false); 
        }
        [TestMethod]
        public void ClearTest()
        {
            string key = "key";
            ICache cache = CacheManager.GetCacher<UnitTest1>();
            cache.Clear(key);
            
        }
        [TestMethod]
        public void ClearTest1()
        {
            string key = "key";
            ICache cache = CacheManager.GetCacher<UnitTest1>();
            cache.Clear();

        }
        [TestMethod]
        public void GetTest1()
        {
            string key = "key1";
            ICache cache = CacheManager.GetCacher<UnitTest1>();
            cache.Set(key, new NameValue { Name = "name3", Value = 111 });
            Type t1 = typeof(NameValue);
            MethodInfo mi = cache.GetType().GetMethod("Get").MakeGenericMethod(new Type[] { t1 });
            var obj = mi.Invoke(cache, parameters: new[] { key });
            //object value = cache.Get<NameValue>(key);

            Assert.AreEqual(obj.GetType(),typeof(NameValue));
        }
        [TestMethod]
        public void GetByReflect0()
        {
            ICache cache = CacheManager.GetCacher<UnitTest1>();
            string key = "key4";
            cache.Set(key, true);
            MethodInfo mi = cache.GetType().GetMethod("Get").MakeGenericMethod(new Type[] { typeof(string) });
            var obj = mi.Invoke(cache, parameters: new[] { "2222" });
            Assert.IsTrue(obj == null);
        }
        [TestMethod]
        public void GetByReflect1()
        {
            ICache cache = CacheManager.GetCacher<UnitTest1>();
            string key = "key4";
            cache.Set(key, true);
            MethodInfo mi = cache.GetType().GetMethod("Get").MakeGenericMethod(new Type[] { typeof(bool) });
            var obj = mi.Invoke(cache, parameters: new[] { "2222" });
            Assert.IsTrue(obj==null);
        }
        [TestMethod]
        public void GetByReflect2()
        {
            ICache cache = CacheManager.GetCacher<UnitTest1>();
            string key = "key4";
            cache.Set(key, true);
            MethodInfo mi = cache.GetType().GetMethod("Get").MakeGenericMethod(new Type[] { typeof(int) });
            var obj = mi.Invoke(cache, parameters: new[] { "2222" });
            Assert.IsTrue(obj == null);
        }
        [TestMethod]
        public void GetByReflect3()
        {
            ICache cache = CacheManager.GetCacher<UnitTest1>();
            string key = "key4";
            cache.Set(key, true);
            MethodInfo mi = cache.GetType().GetMethod("Get").MakeGenericMethod(new Type[] { typeof(DateTime) });
            var obj = mi.Invoke(cache, parameters: new[] { "2222" });
            Assert.IsTrue(obj == null);
        }
        [TestMethod]
        public void GetByReflect4()
        {
            ICache cache = CacheManager.GetCacher<UnitTest1>();
            string key = "key4";
            cache.Set(key, true);
            MethodInfo mi = cache.GetType().GetMethod("Get").MakeGenericMethod(new Type[] { typeof(DateTime?) });
            var obj = mi.Invoke(cache, parameters: new[] { "2222" });
            Assert.IsTrue(obj == null);
        }
        [TestMethod]
        public void GetByReflect5()
        {
            Type type = typeof(DateTime);
            var v= default(DateTime);
            //Assert.IsTrue(type.IsValueType);
            Assert.IsTrue(typeof(NameValue).IsValueType);
        }

    }
    public class NameValue
    {
        public string Name { set; get; }
        public int Value { set; get; }
    }
}
